package IO;

import java.io.*;
import java.util.Scanner;

public class IODemo10 {
    // 将文件拷贝到另一个文件中
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要拷贝文件的的路径: ");
        String srcPath = scanner.next();
        System.out.print("请输入要拷贝到的路径: ");
        String destPath = scanner.next();

        File srcFile = new File(srcPath);
        if (!srcFile.isFile()){
            System.out.println("输入的文件路径有误!");
            return;
        }

        File destFile = new File(destPath);
        if (destFile.isFile()){
            System.out.println("拷贝的路径有误!");
            return;
        }

        try (InputStream inputStream = new FileInputStream(srcFile);
        OutputStream outputStream = new FileOutputStream(destFile)){
            while (true){
                int b = inputStream.read();
                if (b == -1){
                    break;
                }
                outputStream.write(b);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
